home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / utilit~1 / stlogin4.lzh / MODEM.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-30  |  2.0 KB  |  115 lines

  1. /* Miscellaneous functions for use with HAYES smart modems
  2.    Kees Lemmens; Aug 1992
  3.  
  4.    Remarks or suggestions to:
  5.    lemmens@dv.twi.tudelft.nl
  6.  
  7. */
  8.  
  9. #ifdef __PUREC__
  10. #include <tos.h>
  11. #else
  12. #include <osbind.h>
  13. #endif
  14.  
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <time.h>
  18. #include "modem.h"
  19.  
  20. /* Cauxin() reads from stdaux line */
  21. /* Cauxout() writes to stdaux line */
  22. /* Cauxis() tells if a char is available on stdaux */
  23.  
  24. void exit(int stat);
  25.  
  26. #ifdef MINT
  27. void Mysleep(unsigned s);
  28. #else
  29. #define Mysleep(s)
  30. #endif
  31.  
  32. void mod_flush(int echo)
  33. {    char c;
  34.     while(Cauxis() < 0)
  35.     {    Delay(50);
  36.         c=(char)Cauxin();
  37.         if (echo) putchar(c & 0x7f);
  38.     }
  39. }
  40.  
  41. int mod_getc(void)
  42. {    time_t t1,t2,mxtm=4;
  43.  
  44.     time(&t1);
  45.     while (Cauxis() == 0)
  46.     {    Delay(50);
  47.         if( time(&t2) - t1 >= mxtm ) /* wait max 4 sec */
  48.             return 0;
  49.     }
  50.     return Cauxin() & 0x7f;    /* only 7 bits/character */
  51. }
  52.  
  53. void mod_putc(char k)
  54. {    Delay(50);        /* safe delaytime on 2400 baud */
  55.     Cauxout(k);
  56. }
  57.  
  58. void mod_puts(char *cmd)
  59. {    int x;
  60.  
  61.     Delay(50);
  62.     for(x=0;x<strlen(cmd);x++)
  63.     {    Delay(35);
  64.         Cauxout(cmd[x]);
  65.     }
  66. }
  67.     
  68. void mod_gets(char *str,int nr)
  69. {    int cnt=0;
  70.  
  71.     while(*str=mod_getc(),cnt++ < nr)
  72.     {    if(*str == '\15' || *str == '\n')
  73.         {    *str = '\0';
  74.             return;
  75.         }
  76.         str++;
  77.     }
  78. }
  79.  
  80. int mod_wait(char *string,int max_tm,int echo)
  81. {    time_t t1,t2;
  82.     int c,fl;
  83.  
  84.     time(&t1); t1 += max_tm;
  85.     while(fl=0,time(&t2) < t1)
  86.     {
  87.         while(fl < strlen(string))
  88.         {    while(Cauxis() == 0 && time(&t2) < t1)
  89.                 Mysleep(1); /* avoid useless system load */
  90.  
  91.             c=mod_getc();
  92.             if (echo) putchar(c);
  93.             if(c == string[fl])    fl++;
  94.             else                 break;
  95.         }
  96.         if (fl>= strlen(string)) return fl; /* string ok */
  97.     }
  98.     return -1;    /* timeout: string not received !! */
  99. }
  100.  
  101. int mod_reset(void)
  102. {    mod_flush(0);
  103.     mod_puts("\r\natz\r\n");
  104.     return mod_wait("OK",10,0);
  105. }
  106.  
  107. void mod_hangup(void)
  108. {    Delay(1500);        /* guard time before/after escape */
  109.     mod_puts("+++");
  110.     Delay(1500);
  111.     mod_puts("\r\nath0\r\n");    /* hangup modem and reinitialize */
  112.     Delay(1500);
  113.     mod_flush(0);
  114. }
  115.